
const express  = require('express')
const app = express()
const data =[
  {
    "id": "1",
    "name": "商品1",
    "price": 10.99,
    "image": "https://source.unsplash.com/random/150x150",
    "desc": "这是商品1的介绍。",
    hot: true,
    high: true,
    max_order: 2
  },
  {
    "id": "2",
    "name": "商品2",
    "price": 19.99,
    "image": "https://source.unsplash.com/random/150x150",
    "desc": "这是商品2的介绍。",
    hot: false,
    high: true,
    max_order: 5
  },
  {
    "id": "3",
    "name": "商品3",
    "price": 7.5,
    "image": "https://source.unsplash.com/random/150x150",
    "desc": "这是商品3的介绍。",
    hot: true,
    high: true,
    max_order: 1
  },
  {
    "id": "4",
    "name": "商品4",
    "price": 25.99,
    "image": "https://source.unsplash.com/random/150x150",
    "desc": "这是商品4的介绍。",
    hot: false,
    high: false,
    max_order: 1
  },
  {
    "id": "5",
    "name": "商品5",
    "price": 14.5,
    "image": "https://source.unsplash.com/random/150x150",
    "desc": "这是商品5的介绍。",
    hot: false,
    high: false,
    max_order: 1
  }
]

app.all('*', (req, res, next) => {
  res.header('Access-Control-Allow-Origin', '*')
  res.header('Access-Control-Allow-Method', 'GET,POST')
  next()
})
app.get('/products', (re, res) => {
  res.json(data)
})

app.get('/detail/:id', (req, res) => {
  /**
   * post: req.body.id
   * get: req.params.id
   */
  const { id } = req.params
  const detail = data.find(item => item.id === id)
  res.json(detail)
})

app.listen(3000, () => {
  console.log('server at 3000')
})